package com.hancher.plugin.hrnumberaddsub;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lxj.xpopup.XPopup;

import java.math.BigDecimal;

/**
 * 数字加减布局
 */
public class HrNumberAddSub extends RelativeLayout {

    BigDecimal maxNum = new BigDecimal(Integer.MAX_VALUE);
    BigDecimal minNum = BigDecimal.ZERO;
    private TextView tv_hr_num_subtract;
    private TextView tv_hr_change_num;
    private TextView tv_hr_num_add;

    public HrNumberAddSub(Context context) {
        super(context);
        init(context, null);
    }

    public HrNumberAddSub(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public HrNumberAddSub(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HrNumberAddSub);
            maxNum = new BigDecimal(typedArray.getInt(R.styleable.HrNumberAddSub_hr_number_max, Integer.MAX_VALUE));
            minNum = new BigDecimal(typedArray.getInt(R.styleable.HrNumberAddSub_hr_number_min, 0));
        }
        View contentView = inflate(context, R.layout.hr_number_add_sub, this);
        tv_hr_num_subtract = contentView.findViewById(R.id.tv_hr_num_subtract);
        tv_hr_change_num = contentView.findViewById(R.id.tv_hr_change_num);
        tv_hr_num_add = contentView.findViewById(R.id.tv_hr_num_add);
        tv_hr_num_subtract.setOnClickListener(v -> {
            BigDecimal num;
            try {
                num = new BigDecimal(tv_hr_change_num.getText().toString());
            } catch (Exception e) {
                num = BigDecimal.ZERO;
            }
            num = num.subtract(BigDecimal.ONE);
            notifyNum(num);
        });
        tv_hr_num_add.setOnClickListener(v -> {
            BigDecimal num;
            try {
                num = new BigDecimal(tv_hr_change_num.getText().toString());
            } catch (Exception e) {
                num = BigDecimal.ZERO;
            }
            num = num.add(BigDecimal.ONE);
            notifyNum(num);
        });
        tv_hr_change_num.setOnClickListener(v -> {
            new XPopup.Builder(getContext())
//                    .hasStatusBarShadow(true)
//                    .hasNavigationBar(true)
                    // 是否与目标水平居中对齐
                    .isCenterHorizontal(true)
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .atView(tv_hr_change_num)
                    .hasShadowBg(false) // 去掉半透明背景
                    .popupWidth(dp2px(getContext(), 150))
                    .asCustom(new NumberKeyboardPopup(getContext(), tv_hr_change_num, new NumberKeyboardPopup.OnViewClickListener() {
                        @Override
                        public void afterNumberChanged(String s) {

                        }

                        @Override
                        public void onComplete(double lastNum) {
                            notifyNum(BigDecimal.valueOf(lastNum));
                        }
                    }))
                    .show();
        });
    }

    private void notifyNum(BigDecimal num) {
        if (num.compareTo(minNum) < 0) {
            num = minNum;
        }
        if (num.compareTo(maxNum) > 0) {
            num = maxNum;
        }
        num = num.stripTrailingZeros();
        if (onNumberChangeListener != null) {
            onNumberChangeListener.onNumberChange(num);
        }
        tv_hr_change_num.setText(getObjStr(num, "0"));
    }

    private int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static String getObjStr(Object obj, String defaultStr) {
        if (obj == null) {
            return defaultStr;
        }
        if (obj instanceof BigDecimal) {
            if (((BigDecimal) obj).compareTo(BigDecimal.ZERO) == 0) {
                return "0";
            }
            return ((BigDecimal) obj).stripTrailingZeros().toPlainString();
        } else if (obj instanceof String) {
            if (TextUtils.isEmpty((CharSequence) obj)) {
                return defaultStr;
            }
        }
        return obj + "";
    }

    interface OnNumberChangeListener {
        void onNumberChange(BigDecimal num);
    }

    private OnNumberChangeListener onNumberChangeListener;

    public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
        this.onNumberChangeListener = onNumberChangeListener;
    }

    public void setChangeNum(BigDecimal num) {
        if (num.compareTo(maxNum) > 0) {
            num = maxNum;
        }
        if (num.compareTo(minNum) < 0) {
            num = minNum;
        }
        ((TextView) findViewById(R.id.tv_hr_change_num)).setText(getObjStr(num, "0"));
    }
}